package com.jjsc.product.controller;

import com.jjsc.common.core.web.controller.BaseController;
import com.jjsc.common.core.web.domain.AjaxResult;
import com.jjsc.common.core.web.page.TableDataInfo;
import com.jjsc.common.security.utils.SecurityUtils;
import com.jjsc.product.api.domain.Product;
import com.jjsc.product.service.IProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "家具商品管理")
@RequestMapping("/product")
public class ProductController extends BaseController {
    @Autowired
    private IProductService productService;

    @Operation(summary = "查询商品列表")
    @GetMapping("/list")
    public TableDataInfo list(Product product) {
        startPage();
        List<Product> list = productService.selectProductlist(product);
        return getDataTable(list);
    }

    @Operation(summary = "新增商品")
    @PostMapping
    public AjaxResult add(@RequestBody Product product) {
        product.setCreateBy(SecurityUtils.getUsername());
        return toAjax(productService.insertProduct(product));
    }


    @Operation(summary = "根据id获取商品详细信息")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        Product product = productService.selectProductById(id);
        return success(product);
    }

    @Operation(summary = "修改商品")
    @PutMapping
    public AjaxResult edit(@RequestBody Product product) {
        product.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(productService.updateProduct(product));
    }

    @Operation(summary = "批量删除商品")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(productService.deleteProductByIds(ids));
    }
    @Operation(summary = "商品上下架")
    @PutMapping("/updateStatus/{productId}/{status}")
    public AjaxResult updateStatus(@PathVariable Long productId, @PathVariable String status) {
        if (!("1".equals(status) || "0".equals(status))) {
            throw new RuntimeException("状态值错误");
        }
        return toAjax(productService.updateStatus(productId,  status));
    }

}

